//
//  UIButton_Extension.swift
//  swiftProductTemplate
//
//  Created by 刘圣洁 on 2025/6/19.
//

import UIKit

extension UIButton {
    // MARK: - 快捷创建方法
    static func makeSystemButton(
        title: String? = nil,
        font: UIFont = AppTheme.Fonts.callout,
        textColor: UIColor = .systemBlue,
        target: Any?,
        action: Selector,
        toView: UIView
    ) -> UIButton {
        let button = UIButton(type: .system)
        button.setTitle(title, for: .normal)
        button.titleLabel?.font = font
        button.setTitleColor(textColor, for: .normal)
        button.addTarget(target, action: action, for: .touchUpInside)
        toView.addSubview(button)
        return button
    }
    
    static func makePlainButton(
        title: String? = nil,
        font: UIFont = AppTheme.Fonts.callout,
        textColor: UIColor = AppTheme.Colors.textPrimary,
        backgroundColor: UIColor? = nil,
        target: Any?,
        action: Selector,
        toView: UIView
    ) -> UIButton {
        let button = UIButton(type: .custom)
        button.setTitle(title, for: .normal)
        button.titleLabel?.font = font
        button.setTitleColor(textColor, for: .normal)
        button.backgroundColor = backgroundColor
        button.addTarget(target, action: action, for: .touchUpInside)
        toView.addSubview(button)
        return button
    }
    
    static func makeCustomButton(
        title: String? = nil,
        font: UIFont = AppTheme.Fonts.callout,
        textColor: UIColor = .systemBlue,
        toView: UIView
    ) -> UIButton {
        let button = UIButton(type: .system)
        button.setTitle(title, for: .normal)
        button.titleLabel?.font = font
        button.setTitleColor(textColor, for: .normal)
        toView.addSubview(button)
        return button
    }
    
    // MARK: - 链式配置方法
    @discardableResult
    func withTitle(_ title: String?, for state: UIControl.State = .normal) -> Self {
        setTitle(title, for: state)
        return self
    }
    
    @discardableResult
    func withAttributedTitle(_ attributedTitle: NSAttributedString?, for state: UIControl.State = .normal) -> Self {
        setAttributedTitle(attributedTitle, for: state)
        return self
    }
    
    @discardableResult
    func withFont(_ font: UIFont) -> Self {
        titleLabel?.font = font
        return self
    }
    
    @discardableResult
    func withTextColor(_ color: UIColor, for state: UIControl.State = .normal) -> Self {
        setTitleColor(color, for: state)
        return self
    }
    
    @discardableResult
    func withBackgroundColor(_ color: UIColor?, for state: UIControl.State = .normal) -> Self {
        UIGraphicsBeginImageContextWithOptions(CGSize(width: 1, height: 1), false, 0)
        color?.setFill()
        UIRectFill(CGRect(x: 0, y: 0, width: 1, height: 1))
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        
        setBackgroundImage(image, for: state)
        return self
    }
    
    @discardableResult
    func withImage(_ image: UIImage?, for state: UIControl.State = .normal) -> Self {
        setImage(image, for: state)
        return self
    }
    
    @discardableResult
    func withContentInsets(_ insets: UIEdgeInsets) -> Self {
        contentEdgeInsets = insets
        return self
    }
    
    @discardableResult
    func withTitleEdgeInsets(_ insets: UIEdgeInsets) -> Self {
        titleEdgeInsets = insets
        return self
    }
    
    @discardableResult
    func withImageEdgeInsets(_ insets: UIEdgeInsets) -> Self {
        imageEdgeInsets = insets
        return self
    }
    
    @discardableResult
    func withCornerRadius(_ radius: CGFloat) -> Self {
        layer.cornerRadius = radius
        layer.masksToBounds = true
        return self
    }
    
    @discardableResult
    func withBorder(width: CGFloat, color: UIColor) -> Self {
        layer.borderWidth = width
        layer.borderColor = color.cgColor
        return self
    }
    
    @discardableResult
    func withShadow(
        color: UIColor = .black,
        radius: CGFloat = 3,
        offset: CGSize = CGSize(width: 0, height: 2),
        opacity: Float = 0.25
    ) -> Self {
        layer.shadowColor = color.cgColor
        layer.shadowRadius = radius
        layer.shadowOffset = offset
        layer.shadowOpacity = opacity
        layer.masksToBounds = false
        return self
    }
    
    @discardableResult
    func withTintColor(_ color: UIColor) -> Self {
        tintColor = color
        return self
    }
    
    // MARK: - 添加点击事件
    @discardableResult
    func addTapAction(_ target: Any?,action: Selector) -> Self {
        addTarget(target, action: action, for: .touchUpInside)
        return self
    }
    
    // MARK: - 图标与文字布局
    @discardableResult
    func withIconLeft(image: UIImage?, spacing: CGFloat = 8) -> Self {
        guard let image = image else { return self }
        
        setImage(image, for: .normal)
        semanticContentAttribute = .forceLeftToRight
        
        // 调整图标和文字的边距
        let insetAmount = spacing / 2
        imageEdgeInsets = UIEdgeInsets(top: 0, left: -insetAmount, bottom: 0, right: insetAmount)
        titleEdgeInsets = UIEdgeInsets(top: 0, left: insetAmount, bottom: 0, right: -insetAmount)
        contentEdgeInsets = UIEdgeInsets(top: 0, left: insetAmount, bottom: 0, right: insetAmount)
        
        return self
    }
    
    @discardableResult
    func withIconRight(image: UIImage?, spacing: CGFloat = 8) -> Self {
        guard let image = image else { return self }
        
        setImage(image, for: .normal)
        semanticContentAttribute = .forceRightToLeft
        
        // 调整图标和文字的边距
        let insetAmount = spacing / 2
        imageEdgeInsets = UIEdgeInsets(top: 0, left: insetAmount, bottom: 0, right: -insetAmount)
        titleEdgeInsets = UIEdgeInsets(top: 0, left: -insetAmount, bottom: 0, right: insetAmount)
        contentEdgeInsets = UIEdgeInsets(top: 0, left: insetAmount, bottom: 0, right: insetAmount)
        
        return self
    }
    
    @discardableResult
    func withIconTop(image: UIImage?, spacing: CGFloat = 8) -> Self {
        guard let image = image, let titleLabel = titleLabel else { return self }
        
        setImage(image, for: .normal)
        
        // 计算图标和文字的大小
        let imageSize = image.size
        let titleSize = titleLabel.intrinsicContentSize
        
        // 调整图标和文字的边距
        imageEdgeInsets = UIEdgeInsets(
            top: -(titleSize.height + spacing),
            left: 0,
            bottom: 0,
            right: -titleSize.width
        )
        
        titleEdgeInsets = UIEdgeInsets(
            top: 0,
            left: -imageSize.width,
            bottom: -(imageSize.height + spacing),
            right: 0
        )
        
        return self
    }
}
